// pages/card/card.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record: 0,

  },

  bindrecord(e) {
    this.setData({
      record: 0
    })
  },

  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  bindActivation(e) {
    const that = this;
    var ru_id = e.currentTarget.dataset.ru_id;
    wx.showModal({
      cancelText: '想一想',
      complete: (res) => {},
      confirmText: '确认',
      content: '点击确认激活充值卡',
      fail: (res) => {},
      showCancel: true,
      success: (res) => {
        if (res.confirm == true) {
          wx.request({
            url: app.globalData.url + 'Activation',
            data: {
              uid: app.globalData.uid,
              ru_id: ru_id,
            },
            method: 'POST',
            success: function (res) {
              if (res.data.info) {
                wx.showToast({
                  title: '激活成功',
                  duration: 1300,
                  icon: 'none',
                  mask: true,
                })
                setTimeout(() => {
                  wx.navigateTo({
                    url: 'card_list',
                  })
                  that.getMycard()
                }, 1400)
              } else {
                wx.showToast({
                  title: res.data.msg,
                  duration: 1200,
                  icon: 'none',
                  mask: true,
                })
              }
            },
          })
        }
      },
      title: '温馨提示',
    })
  },

  getMycard(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Mycard',
      data: {
        uid: app.globalData.uid,
      },
      method: 'POST',
      success: function (res) {
        if (res.data.info) {
          that.setData({
            info: res.data.info,
          })
          // that.getallprices()
        }
      },
    })
    wx.hideLoading()
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMycard()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getMycard()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMycard()
  },

  bindRu_id(e) {
    var that = this;
    var ru_id = e.currentTarget.dataset.id;
    that.setData({
      record: 1
    })
    wx.request({
      url: app.globalData.url + 'ShareCard',
      data: {
        // uid: app.globalData.uid,
        ru_id:ru_id,
        s_uid: app.globalData.uid,
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading()
        if (res.data.info) {
          that.setData({
            getid: res.data.info,
            ru_id: ru_id,
          })
          that.getMycard()
        }
      },
    })
  },

  bindshare(e) {
    var that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'ShareCard',
      data: {
        uid: app.globalData.uid,
        ru_id: that.data.ru_id,
        s_uid: app.globalData.uid,
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading()
        if (res.data.info) {
          that.setData({
            record: 0,
            getid: res.data.info,
          })
          that.getMycard()
        }
      },
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const that = this;
    var getid = that.data.getid;
    var uid = app.globalData.uid;
    var wx_name = app.globalData.wx_name;
    var shareNum = 1
    if (res.from == 'button') {
      // that.bindshare()
      console.log(getid)
      return {
        title: wx_name + '给您赠送一张充值卡',
        path: 'pages/card/card_list?getid' + getid + "&shareNum=" + shareNum + "&getid=" + that.data.getid,
        // imageUrl: that.data.goods.img[0],
      }
    }
  }
})